package com.banzhi.shop.utils;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import java.text.DecimalFormat;

/**
 * Created by HT on 2017/3/28.
 */

public class NumberUtils {
    //给一个String的数字   转换为0.00格式的字符串
    public static String DecimalFormat(String s) {
        DecimalFormat df = new DecimalFormat("#####0.00");
        try {
            String str = df.format(Double.valueOf(s).doubleValue());
            return str;
        } catch (NumberFormatException e) {
            return "";
        }
    }

    //银行卡
    public static String BankCardToStar(String str) {
        StringBuilder s = new StringBuilder();
        StringBuilder ss = new StringBuilder();
        s.append(str.substring(0, 3));
        for (int i = 3; i < str.length() - 3; i++) {
            s.append("*");
        }
        s.append(str.substring(str.length() - 3));
        int count = 0;
        if (s.length() % 4 == 0) {
            count = s.length() / 4;
        } else {
            count = s.length() / 4 + 1;
        }
        for (int j = 0; j < count; j++) {
            if (j == count - 1) {
                ss.append(s.substring(0 + j * 4, s.length()));
                ss.append("  ");
            } else {
                ss.append(s.substring(0 + j * 4, 4 + j * 4));
                ss.append("  ");
            }

        }
        return ss.toString().trim();
    }

    //输入价格
    public static void setPricePoint(final EditText editText) {

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 3);
                        editText.setText(s);
                        editText.setSelection(s.length());
                    }
                }
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    editText.setText(s);
                    editText.setSelection(2);
                }
                if (s.toString().startsWith("0") && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        editText.setText(s.subSequence(0, 1));
                        editText.setSelection(1);
                        return;
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
            }
        });
    }

    /**
     * 将0-9的数字转换为0+“0-9”
     *
     * @param i
     * @return
     */
    public static String oneTtwo(String i) {
        if ("0".equals(i) || "1".equals(i) || "2".equals(i) || "3".equals(i) || "4".equals(i) || "5".equals(i) || "6".equals(i)
                || "7".equals(i) || "8".equals(i) || "9".equals(i)) {
            return "0" + i;
        }
        return i;
    }

    /**
     * 判断某个字符串是不是数字
     * @param str
     * @return
     */
    public static boolean isInt(String str) {
        if (str.matches("\\d+(.\\d+)?")) {
            return true;
        } else {
            return false;
        }
    }
}
